﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using Inventory.BusinessEntities;
using Locator.Stores;
using DataAccess.BusinessEntities;

namespace Locator.Inventory.Views
{
    public class InventoryPresenter : Presenter<IInventoryView>
    {
        private readonly IInventoryController controller;

        public InventoryPresenter
            (
            [CreateNew] IInventoryController controller
            )
        {
            this.controller = controller;
        }

        public override void OnViewLoaded()
        {
            InventoryRequest request = new InventoryRequest()
            {
                ProductIdType = ProductIdType.AllUsersProducts,
                StoreNumberType = StoreNumberType.AllUsersStores
            };

            var inventory = this.controller.GetInventoriesWithRequest(request);
            this.View.SetSearchResults(inventory);
        }

        public override void OnViewInitialized()
        {
            var stores = this.controller.GetStoresForCurrentUser();
            stores.Insert(0, new Store() { Id = -1, Address = "Your stores" });
            stores.Insert(1, new Store() { Id = -2, Address = "All stores" });
            this.View.SetStores(stores);

            List<ProductListItem> products = this.controller.GetProductsForCurrentUser();
            products.Insert(0, new ProductListItem() { Name = "Your products", Id = -1 });
            this.View.SetProducts(products);
        }

        public void OnSearchClicked()
        {
            StoreNumberType storeNumberType;
            ProductIdType productIdType;
            if (this.View.StoreNumber == -2)
            {
                storeNumberType = StoreNumberType.AllStores;
            }
            else if (this.View.StoreNumber == -1)
            {
                storeNumberType = StoreNumberType.AllUsersStores;
            }
            else
            {
                storeNumberType = StoreNumberType.IndividualStore;
            }

            if (this.View.ProductNumber == -1)
            {
                productIdType = ProductIdType.AllUsersProducts;
            }
            else
            {
                productIdType = ProductIdType.IndividualProduct;
            }

            InventoryRequest request = new InventoryRequest()
            {
                ProductIdType = productIdType,
                StoreNumberType = storeNumberType,
                StoreNumber = this.View.StoreNumber,
                ProductId = this.View.ProductNumber
            };

            var inventory = this.controller.GetInventoriesWithRequest(request);
            this.View.SetSearchResults(inventory);
        }
    }
}




